package edu.gduf.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.gduf.dao.CourseMapper;
import edu.gduf.dto.SigninSystemResult;
import edu.gduf.entity.Course;
import edu.gduf.entity.Teacher;

@RestController
@RequestMapping("/course")
public class CourseController {

	@Autowired
	private CourseMapper courseMapper;

	@PostMapping("/insert")
	public SigninSystemResult insert(String courceId, String courceName, HttpSession session) {
		int cid = 0;
		SigninSystemResult signinSystemResult;
		try {
			cid = Integer.parseInt(courceId);
		} catch (Exception e) {
			signinSystemResult = new SigninSystemResult(false, "课程编号必须为数字!");
			return signinSystemResult;
		}
		Course selectByPrimaryKey = courseMapper.selectByPrimaryKey(cid);
		if (selectByPrimaryKey != null) {
			signinSystemResult = new SigninSystemResult(false, "课程编号已经存在!");
			return signinSystemResult;
		}
		Teacher teacher= (Teacher) session.getAttribute("teacher");
		Course course = new Course();
		course.setCourseName(courceName);
		course.setTid(teacher.gettId());
		course.setCourseId(cid);
		int insert = courseMapper.insert(course);
		if (insert == 0) {
			signinSystemResult = new SigninSystemResult(false, "添加失败,请重试或与管理员联系!");
			return signinSystemResult;
		} else {
			signinSystemResult = new SigninSystemResult(true, "添加成功!");
			return signinSystemResult;
		}
		
	}

	@GetMapping("/getCourseList")
	public Map getCourseList(HttpSession session) {
		Teacher teacher = (Teacher) session.getAttribute("teacher");
		List<Course> courses = courseMapper.selectByTid(teacher.gettId());
		Map hashMap = new HashMap();
		hashMap.put("rows", courses);
		return hashMap;

	}

	@PostMapping("/delete")
	public SigninSystemResult deleteCourses(@RequestParam("pids") List<Integer> pids) {
		SigninSystemResult signinSystemResult = new SigninSystemResult();
		for (Integer pid : pids) {
			courseMapper.deleteByPrimaryKey(pid);

		}
		signinSystemResult.setSuccess(true);
		return signinSystemResult;

	}

	@GetMapping("/updateCourse")
	public void updateCourse(Integer courseId, String courseName, Integer update, Integer oldId,
			HttpServletRequest request, HttpServletResponse response, HttpSession session)
					throws ServletException, IOException {
		request.setAttribute("courseId", courseId);
		request.setAttribute("courseName", courseName);
		if (update == 0) {
			request.getRequestDispatcher("/WEB-INF/jsp/updateCourse.jsp").forward(request, response);
			return;
		} else {
			Teacher teacher = (Teacher) session.getAttribute("teacher");
			courseMapper.deleteByPrimaryKey(oldId);
			Course course = new Course();
			course.setCourseId(courseId);
			course.setCourseName(courseName);
			course.setTid(teacher.gettId());
			courseMapper.insert(course);
		}

		return;
	}

}
